package com.github.rexsheng.mybatis.core;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;

import com.github.rexsheng.mybatis.config.AutoMapperConfiguration;
import com.github.rexsheng.mybatis.util.MybatisExtensionVersion;

/**
 * @author RexSheng
 * 2021年6月8日 下午9:14:13
 * @since 1.6.0
 */
public class AutoMapperScannerRegister implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {

	@SuppressWarnings("unused")
	private ResourceLoader resourceLoader;
	
	private static final String[] BANNER= {
    		"                  _",
    		"      |_  _|_'  _|_  _|_~_   _' _", 
        	"|\\/|\\/|_)(_| |_( |_)( |() |_( |) |",
        	"    /"
    };

	private static final int STRAP_LINE_SIZE = 33;

	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		showVersion();
		RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
		rootBeanDefinition.setBeanClass(AutoMapperConfiguration.class);
		registry.registerBeanDefinition("autoMapperConfiguration", rootBeanDefinition);
	}

	private void showVersion() {
		for (String line : BANNER) {
			System.out.println(line);
		}
		String version = MybatisExtensionVersion.getVersion();
		if (version != null) {
			version = "v" + version;
			StringBuilder padding = new StringBuilder();
			while (padding.length() < STRAP_LINE_SIZE - version.length()) {
				padding.append(" ");
			}
			padding.append(version);
			System.out.println(padding.toString());
		}
		System.out.println();
	}

	@Override
	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}
}
